/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.me.countrycity.SQL;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import org.me.countrycity.utill.LetterToTable;
import org.me.countrycity.utill.eCategories;

/**
 *
 * @author kipi
 */
public class WordsSQL implements IWordsSQL {

    private static String[] SELECT_CATEGORY_COLOM = {Constants.CATAGORY};
    private static String[] SELECT_WORD_COLOM = {Constants.WORD};
    private static String[] SELECT_ALL_COLOMS = {Constants.WORD, Constants.CATAGORY};
    private SQLHelper _sqlHelper;

    public WordsSQL(SQLHelper sqlHelper) {
        _sqlHelper = sqlHelper;
    }

    public void AddWord(String word, eCategories category) {
        SQLiteDatabase db = _sqlHelper.getWritableDatabase();
        ContentValues values = new ContentValues();

        values.put(Constants.WORD, word);
        values.put(Constants.CATAGORY, category.toString());

        db.insertOrThrow(getTable(word), null, values);
        db.close();
    }

    public void AddWords(String[] word, eCategories category) {
        SQLiteDatabase db = _sqlHelper.getWritableDatabase();
        ContentValues values = new ContentValues();

        for (int i = 0; i < word.length; i++) {
            values.put(Constants.WORD, word[i]);
            values.put(Constants.CATAGORY, category.toString());
            db.insertOrThrow(getTable(word[i]), null, values);
        }
        db.close();
    }

    public String[] WordsByLetter(char letter) {
        SQLiteDatabase db = _sqlHelper.getReadableDatabase();
        Cursor cursor = db.query(LetterToTable.getTable(letter), SELECT_WORD_COLOM, null, null, null,
                null, Constants.WORD);
        String[] ret = extractCursor(cursor);
        db.close();
        return ret;
    }

    public String[] WordsByLetterAndCategory(char letter, eCategories category) {
        SQLiteDatabase db = _sqlHelper.getReadableDatabase();
        Cursor cursor = db.query(LetterToTable.getTable(letter), SELECT_WORD_COLOM, Constants.CATAGORY + "='" + category + "'", null, null,
                null, Constants.WORD);
        String[] ret = extractCursor(cursor);
        db.close();
        return ret;
    }

    public boolean IsWordInCategory(String word, eCategories category) {
        String table = getTable(word);
        SQLiteDatabase db = _sqlHelper.getReadableDatabase();
        Cursor cursor = db.query(table, SELECT_WORD_COLOM,Constants.WORD + "='"+ word + "' AND "+ Constants.CATAGORY + "='" + category + "'", null, null,
                null, Constants.WORD);
        String[] ret = extractCursor(cursor);
        db.close();
        return ret.length != 0;
    }

    private String[] extractCursor(Cursor cursor) {
        cursor.moveToNext();
        String[] ret = new String[cursor.getCount()];
        for (int i = 0; i < ret.length; i++) {
            ret[i] = cursor.getString(0);
            cursor.moveToNext();
        }
        cursor.close();
        return ret;
    }

    private String getTable(String word) {
        return LetterToTable.getTable(word.charAt(0));
    }
    private String getTable(char letter) {
        return LetterToTable.getTable(letter);
    }
}
